package org.swim.core.entities;

// Generated Dec 7, 2012 12:57:21 AM by Hibernate Tools 3.4.0.CR1

import static javax.persistence.GenerationType.IDENTITY;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.swim.remote.entities.IMessage;
import org.swim.remote.entities.INotification;
import org.swim.remote.entities.IUser;

/**
 * Message generated by hbm2java
 */
@Entity
@Table(name = "Message", catalog = "jboss")
public class Message extends SwimEntity implements java.io.Serializable,
		IMessage, INotification {

	private static final long serialVersionUID = 1L;

	private Integer id;
	private Date timestamp = new Date(System.currentTimeMillis());
	private Collaboration collaboration;
	private User userBySenderId;
	private User userByReceiverId;
	private String message;
	private NotificationStatus notificationStatus = NotificationStatus.UNREAD;

	public Message() {
	}

	public Message(Collaboration collaboration, String message) {
		this.collaboration = collaboration;
		this.message = message;
	}

	public Message(Collaboration collaboration, User userBySenderId,
			User userByReceiverId, String message) {
		this.collaboration = collaboration;
		this.userBySenderId = userBySenderId;
		this.userByReceiverId = userByReceiverId;
		this.message = message;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.swim.entities.IMessage#getId()
	 */
	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id", unique = true, nullable = false)
	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	// @Version
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.swim.entities.IMessage#getTimestamp()
	 */
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "ts", nullable = false, length = 19)
	public Date getTimestamp() {
		return this.timestamp;
	}

	public void setTimestamp(Date timestamp) {
		this.timestamp = timestamp;
	}

	@Enumerated(EnumType.STRING)
	@Column(name = "notificationStatus", nullable = false, columnDefinition = "ENUM('UNREAD', 'READ', 'HIDDEN'")
	// , length = 2)
	public NotificationStatus getNotificationStatus() {
		return notificationStatus;
	}

	public void setNotificationStatus(NotificationStatus notificationStatus) {
		this.notificationStatus = notificationStatus;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.swim.entities.IMessage#getCollaboration()
	 */
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "collaborationId", nullable = false)
	public Collaboration getCollaboration() {
		return this.collaboration;
	}

	public void setCollaboration(Collaboration collaboration) {
		this.collaboration = collaboration;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.swim.entities.IMessage#getUserBySenderId()
	 */
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "senderId")
	public User getUserBySenderId() {
		return this.userBySenderId;
	}

	public void setUserBySenderId(User userBySenderId) {
		this.userBySenderId = userBySenderId;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.swim.entities.IMessage#getUserByReceiverId()
	 */
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "receiverId")
	public User getUserByReceiverId() {
		return this.userByReceiverId;
	}

	public void setUserByReceiverId(User userByReceiverId) {
		this.userByReceiverId = userByReceiverId;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.swim.entities.IMessage#getMessage()
	 */
	@Column(name = "message", nullable = false, length = 65535)
	public String getMessage() {
		return this.message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@Transient
	public IUser getIUserBySenderId() {
		// Collaboration collaboration = getCollaboration();
		IUser user = getUserBySenderId();
		//
		// if (collaboration.getSenderVisibility() == Visibility.HIDDEN)
		// if (user != null && user.equals(collaboration.getUserBySenderId()))
		// user = null;

		return user;
	}

	@Transient
	public IUser getIUserByReceiverId() {
		IUser user = getUserByReceiverId();
		return user;
	}

	@Transient
	public String getNotificationType() {
		return Message.class.getSimpleName();
	}

}
